<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Dep extends Controller
{
    private $_model;
    private $title_field = [

    ];
    public $pagesize = 10000;

    public function _initialize()
    {
         (new \app\admin\behavior\CheckPermissions())->run();//增加权限控制
        $this->_model = new \app\admin\model\Dep();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);
        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
    }

    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {

        return $this->fetch('index');
    }


    public function recordSystemLog($arr)
    {
        $model = new \app\admin\model\Log();
        $model->add($arr);
    }

    public function lists()
    {

        $ser_key = trim(input("request.ser_key"));//auth
        $ser_val = trim(input("request.ser_val"));//auth


        if ($ser_key && $ser_val) {
            $where[$ser_key] = array('like', '%' . $ser_val . '%');
        }


        $where['level'] = 0;
        $lists = $this->_model->lists($where, $this->pagesize);

        $this->assign('lists', $lists);

        $this->assign('request', input("request."));
        return view('lists');

    }


    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }

        $oldGroup = $this->_model->getDataForWhere(array(
            'id' => $id
        ));

        $itemGroup = $this->_model->getDataForWhere(array(
            'father' => $id
        ));
        if ($itemGroup) {
            $return = ['err' => 3, 'msg' => '该部门下存在二级部门，暂不能删除'];
            return json($return);
        }

        $res = $this->_model->delData(array('id' => $id));

        if ($res) {

            //操作日志记录
            //$userInfo =$this->_model->getID($id);
            $action = "删除顶级部门[" . $oldGroup['depname'] . "][id:" . $id . "]";

            $this->recordSystemLog([
                'user' => $this->LoginUserInfo['UserInfo']['id'],
                'time' => date("Y-m-d H:i:s"),
                'action' => $action,
                'type' => 4,
            ]);
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }


    }


    public function addData()
    {
        $time = time();
        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $depname = trim(input("request.depname"));//auth
            if (empty($depname)) {

                $return = ['err' => 3, 'msg' => '对不起部门名称不能为空'];
                return json($return);
            }

            $oldGroup = $this->_model->getDataForWhere(array(
                'depname' => $depname,
                'level' => 0,
            ));
            if (!empty($oldGroup)) {
                $return = ['err' => 3, 'msg' => '该部门已经存在，请不要重复添加'];
                return json($return);
            }

            $insertData = array(
                'depname' => $depname,

                'ctime' => $time,
            );
            $res = $this->_model->add($insertData);

            if ($res) {
                //操作日志记录
                //$userInfo =$this->_model->getID($id);
                $action = "添加顶级部门[" . $insertData['depname'] . "][id:" . $res . "]";

                $this->recordSystemLog([
                    'user' => $this->LoginUserInfo['UserInfo']['id'],
                    'time' => date("Y-m-d H:i:s"),
                    'action' => $action,
                    'type' => 4,
                ]);
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }


        //设置全局信息
        $title = "添加部门";
        $this->assign('title', $title);

        return view('addData');

    }


    public function editData()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $depname = trim(input("request.depname"));//auth
            if (empty($depname)) {

                $return = ['err' => 3, 'msg' => '对不起部门名称不能为空'];
                return json($return);
            }
            $oldGroup = $this->_model->getDataForWhere(array(
                'depname' => $depname,
                'level' => 0,
            ));

            if (!empty($oldGroup)) {
                if ($oldGroup['id'] != $id) {
                    $return = ['err' => 3, 'msg' => '对不起,该部门名称已经存在'];
                    return json($return);
                }

            }
            $w = array(
                'id' => $id
            );
            $update = array(
                'depname' => $depname,
            );
            $res = $this->_model->updateDataForWhere($update, $w);

            if ($res || ($res === 0)) {
                //记录操作日志
                $action = "修改顶级部门[" . $update['depname'] . "][id:" . $id . "]";

                $this->recordSystemLog([
                    'user' => $this->LoginUserInfo['UserInfo']['id'],
                    'time' => date("Y-m-d H:i:s"),
                    'action' => $action,
                    'type' => 4,
                ]);
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }

        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));
        $this->assign('info', $info);


        //设置全局信息
        $title = "编辑部门";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('editData');

    }


}